require "csv"

class Hand
    attr_accessor :cards

    def initialize(cards)
        @cards = cards
    end

    def howMany(alphanumberic)
        howMany = 0
        @cards.each do |card|
            if String.new.concat(card[0]).eql?(alphanumberic) || String.new.concat(card[1]).eql?(alphanumberic)
                howMany += 1
            end
        end
        howMany
    end

    def highCards()
        numbers = ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"]
        @cards.sort! { |a, b|
            numbers.index(String.new.concat(a[0])) <=> numbers.index(String.new.concat(b[0]))
        }
        @cards.reverse
    end


    def pairs()
        numbers = ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"]
        pairsArray = []
        numbers.each do |number|
            if howMany(number) == 2
                pairsArray << number
            end
        end
        pairsArray
    end

    def howManyPairs()
        pairs.length
    end

    def hasOnePair()
        onePair != nil
    end

    def onePair()
        pair = nil
        if pairs.length == 1
            pair = pairs
        end
        pair
    end

    def HasTwoPair()
        twoPair != nil
    end

    def twoPair
        twoPair = nil
        if pairs.length == 2
            twoPair = pairs
        end
        twoPair
    end

    def hasThreeOfAKind()
        threeOfAKind != nil
    end

    def threeOfAKind()
        numbers = ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"]
        threeOfAKind = nil
        numbers.each do |number|
            if howMany(number) == 3
                threeOfAKind = number
            end
        end
        threeOfAKind
    end

    def hasStraight
        straight != nil
    end

    def straight()
        numbers = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"]
        numbers.each_with_index do |number, index|
            if howMany(number) > 0
                yes = true
                if index + 4 < numbers.length
                    (index + 1..index + 4).each do |nextNumber|
                        if howMany(numbers[nextNumber]) > 0
                            yes = yes && true
                        else
                            yes = yes && false
                        end
                    end
                    if yes
                        return numbers[index + 4]
                    end
                end
            end
        end
        nil
    end

    def hasFlush()
        flush != nil
    end

    def flush()
        suits = ["C", "D", "H", "S"]
        flushSuit = nil
        suits.each do |suit|
            if howMany(suit) >= 5
                flushSuit = suit
            end
        end
        flushSuit
    end

    def hasFullHouse
        hasOnePair && hasThreeOfAKind
    end

    def fullHouse()
        cards = []
        cards << onePair
        cards << threeOfAKind
    end

    def hasFourOfAKind
       fourOfAKind != nil
    end

    def fourOfAKind
        numbers = ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"]
        numbers.each do |number|
            if howMany(number) == 4
                return number
            end
        end
        nil
    end

    def hasStraightFlush
        straightFlush != nil
    end

    def straightFlush
        if hasStraight && hasFlush
            return straight
        end
        nil
    end
end

#hand = Hand.new(["QC", "AD", "QH", "QD", "AH"])
#puts hand.onePair
#puts hand.twoPair
#puts hand.hasThreeOfAKind
#puts hand.flush
#puts hand.hasFullHouse
#puts hand.fourOfAKind
#puts hand.highCards
#puts hand.straight

hands = CSV.read "poker.txt"
hands.each_with_index do |hand, index|
    player1 = []
    (0..4).each do |index2|
        player1 << hand[index2]
    end
    cards = Hand.new(player1)
    if cards.hasStraightFlush
        puts index.to_s +" straightFlush " + cards.straightFlush[0] + cards.straightFlush[1]
    elsif cards.hasFourOfAKind
        puts index.to_s + "4OfAKind " + cards.fourOfAKind
    elsif cards.hasFullHouse
        puts index.to_s + "fullHouse " + cards.fullHouse[0] + cards.fullHouse[1]
    elsif cards.hasFlush
        puts index.to_s + "flush " + cards.flush
    elsif cards.hasStraight
        puts index.to_s + "straight " + cards.straight
    elsif cards.hasThreeOfAKind
        puts index.to_s + "threeOfAKind " + cards.threeOfAKind
    elsif cards.HasTwoPair
        puts index.to_s + "two pair" + cards.twoPair[0] + cards.twoPair[1]
    elsif cards.hasOnePair
        puts index.to_s + "one pair " + cards.onePair[0]
    else
        puts index.to_s + "highCard " + cards.highCards[0]
    end

    player2 = []
    (5..9).each do |index|
        player2 << hand[index]
    end
    cards2 = Hand.new(player2)
#    puts cards2.howMany('A')
end